package com.mfz.common.units

import com.alibaba.fastjson.JSONObject
import okhttp3.*
import org.apache.logging.log4j.LogManager
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.Charset


private val log = LogManager.getLogger("com.mfz.serviceProvider.http")

fun okPostNoCache(client: OkHttpClient, url: String, text: String, soapAction: String? = null, charset: String = "utf-8", contentType: String = "text/xml", block: (String) -> JSONObject): JSONObject {
	log.trace("发送请求:$text")

	val type = MediaType.parse("$contentType; charset=$charset")
	val body = RequestBody.create(type, text)

	val requestBuilder = Request.Builder()
		.cacheControl(CacheControl.Builder().noCache().build())
		.url(url)
		.method("POST", body)

	if (soapAction != null)
		requestBuilder.addHeader("SOAPAction", soapAction)
	val request = requestBuilder.build()
	val response = client.newCall(request).execute()

	val summer = response.body()?.string() ?: Int.MIN_VALUE.toString()
	log.trace("得到返回值: $summer")

	return block(summer)
}

fun postByOk(client: OkHttpClient, url: String, text: String, headers: List<Pair<String, String>>? = null, charset: String = "utf-8", contentType: String = "text/xml", block: (String) -> JSONObject): JSONObject {
	log.trace("发送请求:$text")

	val type = MediaType.parse("$contentType; charset=$charset")
	val body = RequestBody.create(type, text)

	val requestBuilder = Request.Builder()
		.cacheControl(CacheControl.Builder().noCache().build())
		.url(url)
		.method("POST", body)

	headers?.forEach { requestBuilder.addHeader(it.first, it.second) }

	val request = requestBuilder.build()
	val response = client.newCall(request).execute()

	val summer = response.body()?.string() ?: Int.MIN_VALUE.toString()
	log.trace("得到返回值: $summer")

	return block(summer)
}


/**
 *code:编码类型
 */
fun post(url: URL, contentType: String, body: String, code: Charset = Charsets.UTF_8, headers: Map<String, String>? = null, SOAPAction: String? = null, connectTimeout: Int = 3000, readTimeout: Int = 3000, block: (String) -> JSONObject): JSONObject {
	val conn = url.openConnection() as HttpURLConnection

	conn.doOutput = true
	conn.doInput = true
	conn.useCaches = false
	conn.connectTimeout = connectTimeout
	conn.readTimeout = readTimeout
	headers?.forEach { k, v -> conn.setRequestProperty(k, v) }
	conn.setRequestProperty("Content-type", contentType)
	if (SOAPAction != null) conn.setRequestProperty("SOAPAction", SOAPAction)
	conn.requestMethod = "POST"

	val out = OutputStreamWriter(conn.outputStream)
	//调整编码格式
	out.append(String(body.toByteArray(Charsets.UTF_8), code))

	log.trace("发送消息:$body")
	out.flush()
	out.close()

	val summer = conn.inputStream.reader(code).readText()
	log.trace("得到返回值: $summer")

	return block(summer)
}

fun getByOK(client: OkHttpClient, url: String, block: (String) -> JSONObject): JSONObject {
	log.trace("发送请求:$url")

	val request = Request.Builder().get().url(url).build()
	val response = client.newCall(request).execute()

	val summer = response.body()?.string() ?: Int.MIN_VALUE.toString()
	log.trace("得到返回值: $summer")

	return block(summer)
}

fun getByOK4String(client: OkHttpClient, url: String, block: (String) -> String?): String? {
	log.trace("发送请求:$url")

	val request = Request.Builder().get().url(url).build()
	val response = client.newCall(request).execute()

	val summer = response.body()?.string() ?: Int.MIN_VALUE.toString()
	log.trace("得到返回值: $summer")

	return block(summer)
}
